package com.maddox.relax.helper;

import android.content.Context;
import android.net.Uri;
import android.provider.MediaStore;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.loader.content.CursorLoader;

import com.maddox.relax.model.ImageSet;
import com.maddox.relax.model.MediaStoreConstants;
import com.maddox.relax.model.MimeType;

import java.util.ArrayList;
import java.util.Set;

import static com.maddox.relax.model.MediaStoreConstants.*;

/**
 * 重写数据集加载器
 */
public class MediaInfoLoader extends CursorLoader {
    //查询的uri路径
    private static final Uri QUERY_URI = MediaStore.Files.getContentUri("external");
    //数据集查询的字段
    private static final String[] PROJECTION = {
            _ID,
            DATA,
            DISPLAY_NAME,
            WIDTH,
            HEIGHT,
            MIME_TYPE,
            SIZE,
            DURATION,
            DATE_MODIFIED
    };
    //根据最后修改时间的顺序来加载
    private static final String ORDER_BY = MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC";

    private MediaInfoLoader(@NonNull Context context, String selection, String[] selectionArgs) {
        super(context, QUERY_URI, PROJECTION, selection, selectionArgs, ORDER_BY);
    }

    /**
     * @param context  上下文环境
     * @param imageSet 查询的文件类型,参考 {@link com.maddox.relax.model.ImageSet}
     * @return
     */
    public static CursorLoader newInstance(Context context, ImageSet imageSet, Set<MimeType> mimeTypeSet) {
        String selections = "";
        String mimeSelections = "";
        int index = 0;
        //要查询的资源类型
        ArrayList<String> mimeTypeList = MimeType.getMimeTypeList(mimeTypeSet);
        String[] selectionArgs = new String[mimeTypeList.size()];
        //所有的图片+视频资源
        if (imageSet.isAllMedia()) {
            for (String mimeType : mimeTypeList) {
                selectionArgs[index] = mimeType;
                mimeSelections = String.format("%s = ? or %s", MediaStore.Files.FileColumns.MIME_TYPE, mimeSelections);
                index++;
            }
            if (mimeSelections.endsWith(" or ")){
                mimeSelections = mimeSelections.substring(0,mimeSelections.length()-4);
            }
            selections = "(" + MEDIA_TYPE + "=" + MEDIA_TYPE_IMAGE + " or " + MEDIA_TYPE + "=" + MEDIA_TYPE_VIDEO + ") and " + SIZE + ">0" + " and (" + mimeSelections + ")";
            //所有图片资源
        } else if (imageSet.isAllImage()) {
            for (String mimeType : mimeTypeList) {
                selectionArgs[index] = mimeType;
                mimeSelections = String.format("%s = ? or %s", MediaStore.Files.FileColumns.MIME_TYPE, mimeSelections);
                index++;
            }
            if (mimeSelections.endsWith(" or ")){
                mimeSelections = mimeSelections.substring(0,mimeSelections.length()-4);
            }
            selections = "(" + MEDIA_TYPE + "=" + MEDIA_TYPE_IMAGE + ") and " + SIZE + ">0" + " and (" + mimeSelections + ")";
            //所有视频资源
        } else if (imageSet.isAllVideo()) {
            for (String mimeType : mimeTypeList) {
                selectionArgs[index] = mimeType;
                mimeSelections = String.format("%s = ? or %s", MediaStore.Files.FileColumns.MIME_TYPE, mimeSelections);
                index++;
            }
            if (mimeSelections.endsWith(" or ")){
                mimeSelections = mimeSelections.substring(0,mimeSelections.length()-4);
            }
            selections = "(" + MEDIA_TYPE + "=" + MEDIA_TYPE_VIDEO + ") and " + SIZE + ">0" + " and (" + mimeSelections + ")";
        }

        return new MediaInfoLoader(context, selections, selectionArgs);

    }


}
